﻿using System;
using System.Collections.Generic;
using System.Text;
using BigfootDNN.Mvc;
using DotNetNuke.Entities.Modules;

namespace BigfootDNN
{
    /// <summary>
    /// Aids in the creation of client urls
    /// </summary>
    public class UrlHelper
    {

        /// <summary>
        /// This is the route information for the curren request
        /// </summary>
        private RouteInfo Route;

        /// <summary>
        /// Creates a new UrlHelper by specifying the route. The route must also have an app attached
        /// </summary>
        /// <param name="route"></param>
        public UrlHelper(RouteInfo route)
        {
            Route = route;
        }

        public UrlHelper(string controller, string action, ModuleInfo modInfo, DnnApp app)
        {
            Route = new RouteInfo(controller, action, modInfo, app);
        }

        // Client URL Alias functions. Used for convenience
        /// <summary>
        /// Gets a browser usable path to a content file within the system. It uses the extion of the file to determine the location of the static file
        /// within the content folder.
        /// i.e. "image.jpg" will return "/desktopmodules/modulename/content/images/image.jpg"
        /// i.e. "menu/left.jpg" will return "/desktopmodules/modulename/content/images/menu/left.jpg"
        /// i.e. "test.js" will return "/desktopmodules/modulename/content/js/test.js"
        /// </summary>
        /// <param name="filename">The name of the file relative to the content folder i.e. left.jpg or menu/left.jpg</param>
        /// <returns>A url that may be used from the browser to access the static resource</returns>
        public string StaticUrl(string filename) { return Route.App.StaticUrl(filename); }

        /// <summary>
        /// Gets a browser usable path to a content file within the system. It uses the extion of the file to determine the location of the static file
        /// within the content folder.
        /// i.e. "image.jpg" will return "/desktopmodules/modulename/content/images/image.jpg"
        /// i.e. "menu/left.jpg" will return "/desktopmodules/modulename/content/images/menu/left.jpg"
        /// i.e. "test.js" will return "/desktopmodules/modulename/content/js/test.js"
        /// </summary>
        /// <param name="filename">The name of the file relative to the content folder i.e. left.jpg or menu/left.jpg</param>
        /// <param name="fullUrl">Determines weather to include the domain name in the url. (i.e. http://domain.com/desktopmodules/modulename/content/image.jpg)</param>
        /// <returns>A url that may be used from the browser</returns>
        public string StaticUrl(string filename, bool fullUrl) { return Route.App.StaticUrl(filename, fullUrl); }

        /// <summary>
        /// Gets a browser usable path to a MVC controller action.
        /// </summary>
        /// <param name="controller">The name of the controller that contains the action</param>
        /// <param name="action">The name of the action within the controller</param>
        /// <returns>A url that may be used from the browser to access the static resource</returns>
        public string MvcUrl(string controller, string action) { return Route.App.MvcUrl(controller, action, Route.PortalId, Route.TabId, Route.ModuleId, string.Empty, false); }

        /// <summary>
        /// Gets a browser usable path to a MVC controller action.
        /// </summary>
        /// <param name="controller">The name of the controller that contains the action</param>
        /// <param name="action">The name of the action within the controller</param>
        /// <param name="querystring">Appends this value to the querystring </param>
        /// <returns>A url that may be used from the browser</returns>
        public string MvcUrl(string controller, string action, string querystring) { return Route.App.MvcUrl(controller, action, Route.PortalId, Route.TabId, Route.ModuleId, querystring, false); }

        /// <summary>
        /// Gets a browser usable path to a MVC controller action.
        /// </summary>
        /// <param name="controller">The name of the controller that contains the action</param>
        /// <param name="action">The name of the action within the controller</param>
        /// <param name="querystring">Appends this value to the querystring </param>
        /// <param name="fullUrl">Determines weather to append the domain name to the url</param>
        /// <returns>A url that may be used from the browser</returns>
        public string MvcUrl(string controller, string action, string querystring, bool fullUrl) { return Route.App.MvcUrl(controller, action, Route.PortalId, Route.TabId, Route.ModuleId, querystring, fullUrl); }

        /// <summary>
        /// Gets a browser usable path to a MVC controller action for AJAX requests. It activates the ajax route handler.
        /// </summary>
        /// <param name="controller">The name of the controller that contains the action</param>
        /// <param name="action">The name of the action within the controller</param>
        /// <returns>A url that may be used from the browser to make an ajax request</returns>
        public string MvcAjaxUrl(string controller, string action) { return Route.App.MvcAjaxUrl(controller, action, Route.PortalId, Route.TabId, Route.ModuleId, string.Empty); }

        /// <summary>
        /// Gets a browser usable path to a MVC controller action for AJAX requests. It activates the ajax route handler.
        /// </summary>
        /// <param name="controller">The name of the controller that contains the action</param>
        /// <param name="action">The name of the action within the controller</param>
        /// <param name="querystring">Appends this value to the querystring </param>
        /// <returns>A url that may be used from the browser to make an ajax request</returns>
        public string MvcAjaxUrl(string controller, string action, string querystring) { return Route.App.MvcAjaxUrl(controller, action, Route.PortalId, Route.TabId, Route.ModuleId, querystring); }


        /// <summary>
        /// This is a shortcut to DotNetNuke's NavigateUrl. It gets the url path to activate a certain DNN control within your module.
        /// </summary>
        /// <param name="controlName">The control key within your module delcaration</param>
        /// <returns>A url that may be used from the browser</returns>
        public string ControlUrl(string controlName) { return Route.App.ControlUrl(Route.ModuleId, controlName, string.Empty); }

        /// <summary>
        /// This is a shortcut to DotNetNuke's NavigateUrl. It gets the url path to activate a certain DNN control within your module.
        /// </summary>
        /// <param name="controlName">The control key within your module delcaration</param>
        /// /// <param name="querystring">Appends this value to the querystring </param>
        /// <returns>A url that may be used from the browser</returns>
        public string ControlUrl(string controlName, string querystring) { return Route.App.ControlUrl(Route.ModuleId, controlName, querystring); }
    }
}
